package com.example.camproject

import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import com.google.gson.Gson
import android.accounts.Account
import android.os.Handler
import android.widget.Button
import android.widget.ImageView
import android.widget.TextView
import android.widget.Toast
import com.bumptech.glide.Glide
import com.example.camproject.network.AccessData
import com.example.camproject.network.Data
import com.example.camproject.network.NetClient
import com.example.camproject.network.User
import retrofit2.Call
import retrofit2.Response


class MainActivity : AppCompatActivity() {
    lateinit var avata:ImageView
    lateinit var username:TextView
    lateinit var accessBtn:Button
    lateinit var loginOut:Button
    lateinit var noaccess:TextView
    lateinit var userdata : Data


    override fun onCreate(savedInstanceState : Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        avata=findViewById(R.id.avata)
        username=findViewById(R.id.username)
        accessBtn=findViewById(R.id.access)
        loginOut=findViewById(R.id.loginout)
        //记住还有关门功能
        noaccess=findViewById(R.id.noaccess)
        checkLogin()
        loginOut.setOnClickListener {
            println("xxxx")
            var udb=getSharedPreferences("userinfo", MODE_PRIVATE)
            var edit=udb.edit()
            edit.clear()
            edit.commit()
            goLogin()
        }
    }

    fun goLogin(){
        var inten1=Intent(this,LoginActivity::class.java)
        startActivity(inten1)
        finish()
    }

    fun checkLogin(){
        var udb=getSharedPreferences("userinfo", MODE_PRIVATE)
        var data=udb.getString("user","null")
        if(data=="null"){
            goLogin()
        }else{
//            val account : `val` = gson.fromJson(json , Account::class.java)
//            println(receiveAccount.toString())
            var gson=Gson()
            userdata=gson.fromJson(data,Data::class.java)
            renderData()
        }
    }

    fun initFunction(){
        accessBtn.setOnClickListener {
            var map1= mapOf("username" to userdata.username)
            var pt=NetClient.apis.postAppBtn(map1)
            pt.enqueue(object :retrofit2.Callback<AccessData>{
                override fun onResponse(call : Call<AccessData> , response : Response<AccessData>) {
                    var res=response.body()
                    if (res!=null && res.code==0){
                        Toast.makeText(this@MainActivity,"请尽快通过",Toast.LENGTH_SHORT).show()
                        val handler=Handler()
                        handler.postDelayed(object :Runnable{
                            override fun run() {
                                var mp= mapOf("username" to "root")
                                var pt2=NetClient.apis.closeWay(mp)
                                solvept2(pt2)
                            }

                        },6000)
                    }else{
                        Toast.makeText(this@MainActivity,"参数或者其他错误",Toast.LENGTH_SHORT).show()
                    }
                }

                override fun onFailure(call : Call<AccessData> , t : Throwable) {
                    Toast.makeText(this@MainActivity,"no network",Toast.LENGTH_SHORT).show()
                }

            })
        }
    }

    fun solvept2(pt2:Call<AccessData>){
        pt2.enqueue(object:retrofit2.Callback<AccessData>{
            override fun onResponse(call : Call<AccessData> , response : Response<AccessData>) {
                var res=response.body()
                if (res!=null &&res.code==0){
                    Toast.makeText(this@MainActivity,"关门",Toast.LENGTH_SHORT).show()
                }
            }

            override fun onFailure(call : Call<AccessData> , t : Throwable) {
                Toast.makeText(this@MainActivity,"no network",Toast.LENGTH_SHORT).show()
            }

        })
    }

    fun renderData(){
        Glide.with(this).load("http://120.76.201.116:8888"+userdata.imgurl).into(avata);
        username.text=userdata.username
        if (userdata.access){
            accessBtn.visibility=Button.VISIBLE
        }else{
            accessBtn.visibility=Button.INVISIBLE
            noaccess.visibility=TextView.VISIBLE
        }
        initFunction()
    }
}